package com.farmpost.server.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.farmpost.server.mapper.FollowerMapper;
import com.farmpost.server.mapper.UserInfoMapper;
import com.farmpost.server.pojo.Follower;
import com.farmpost.server.pojo.RespBean;
import com.farmpost.server.pojo.UserInfo;
import com.farmpost.server.service.IFollowerService;
import com.farmpost.server.service.IUserInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

import javax.print.DocFlavor;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author king
 * @since 2022-04-12
 */
@Service
public class FollowerServiceImpl extends ServiceImpl<FollowerMapper, Follower> implements IFollowerService {

    @Autowired
    private FollowerMapper followerMapper;
    @Autowired
    private IUserInfoService iUserInfoService;

    /**
     * 根据当前用户获取粉丝列表
     * @return
     */
    @Override
    public List<Follower> getFollower() {
        UserInfo user = (UserInfo) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        List<Follower> followers = followerMapper.selectList(new QueryWrapper<Follower>().eq("user_id", user.getUserId()));
        for (Follower follower : followers) {
            Integer followerId = follower.getFollower();
            UserInfo userInfo = iUserInfoService.getUserInfoByUserId(followerId);
            if (userInfo != null)
                userInfo.setUserPassword(null);
            follower.setUserInfo(userInfo);
        }
        return followers;
    }

    /**
     * 删除粉丝
     * @param followerId
     * @return
     */
    @Override
    public RespBean deleteFollower(Integer followerId) {
        int deleteFollower = followerMapper.deleteFollower(3, LocalDateTime.now(), followerId);
        if (deleteFollower == 1)
            return RespBean.success("粉丝删除成功");
        return RespBean.error("粉丝删除失败");
    }

    /**
     * 添加粉丝
     * @param userId
     * @param followerId
     * @return
     */
    @Override
    public RespBean insertFollower(Integer userId, Integer followerId) {
        //判断对应的用户和粉丝是否存在
        UserInfo userInfo = iUserInfoService.getUserInfoByUserId(userId);
        if (userInfo.getUserId() == null)
            return RespBean.error("用户不存在");
        UserInfo getFollowerId = iUserInfoService.getUserInfoByUserId(followerId);
        if (getFollowerId.getUserId() == null)
            return RespBean.error("粉丝用户不存在");
        Follower follower= new Follower();
        follower.setUserId(userId);
        follower.setFollower(followerId);
        follower.setStatus(1);
        follower.setCreateTime(LocalDate.now());
        follower.setUpdateTime(LocalDateTime.now());
        int insert = followerMapper.insert(follower);
        if (insert == 1)
            return RespBean.success("插入粉丝成功");
        return RespBean.error("插入粉丝失败");

    }

    /**
     * 修改粉丝信息
     * @param userId
     * @param followerId
     * @param status
     * @return
     */
    @Override
    public RespBean updateFollower(Integer userId, Integer followerId, Integer status) {
        UserInfo userInfoByUserId = iUserInfoService.getUserInfoByUserId(userId);
        if (userInfoByUserId.getUserId() == null)
            return RespBean.error("要更改的用户不存在");
        if (followerId != null && status == null) {
            int updateFollowerId = followerMapper.updateFollowerId(userId, followerId, LocalDateTime.now());
            if (updateFollowerId == 1)
                return RespBean.success("更改粉丝id成功");
            return RespBean.error("更改粉丝id失败");
        }else if (status != null && followerId == null) {
            int updateFollowerStatus = followerMapper.updateFollowerStatus(userId, status, LocalDateTime.now());
            if (updateFollowerStatus == 1)
                return RespBean.success("更改粉丝状态成功");
            return RespBean.error("更改粉丝状态失败");
        }else if (followerId != null && status != null) {
            int updateFollowerIdStatus = followerMapper.updateFollowerIdStatus(userId, followerId, status, LocalDateTime.now());
            if (updateFollowerIdStatus == 1)
                return RespBean.success("更改粉丝信息成功");
            return RespBean.error("更改粉丝信息失败");
        }else {
            return RespBean.error("未输入要更改的粉丝信息");
        }
    }
}
